package com.ys.peony.car300;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ys.peony.R;
import com.ys.peony.app.YSConstant;
import com.ys.peony.base.BaseFragment;
import com.ys.peony.base.BasicFragment;
import com.ys.peony.base.contract.CarPledgeContract;
import com.ys.peony.enums.SwipeDirect;
import com.ys.peony.model.domain.CarDetailModel;
import com.ys.peony.presenter.CarPledgePresenter;
import com.ys.peony.ui.activity.CarPledgeActivity;
import com.ys.peony.ui.widget.AlertTwoBtnDialog;
import com.ys.peony.ui.widget.CustomAlertDialogCallBack;
import com.ys.peony.utils.LoadingProgressUtils;
import com.ys.peony.utils.ToastUtils;
import com.ys.peony.utils.YsUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import com.yklab.library.refreshlayout.adapters.CommonRecyclerViewAdapter;
import com.yklab.library.refreshlayout.holder.CommonRecyclerViewHolder;
import com.yklab.library.refreshlayout.inter.OnClickCommonListener;

/**
 * Created by 59153 on 2017/8/7.
 */

public class BaoDanFragment extends BasicFragment<CarPledgeContract.iBaodanView, CarPledgePresenter> implements View.OnClickListener,CarPledgeContract.iBaodanView {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;

    @BindView(R.id.moment_iv_edit)
    LinearLayout moment_iv_edit;

    private CommonRecyclerViewAdapter mAdapter;
    private List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean> mList;
    private LinearLayoutManager mLayoutManager;

    private int mPosition = YSConstant.INVALID; // 标记当前车辆index;

    private int mOperateType = YSConstant.INVALID;// 标记当前是查看页面还是修改页面

    @Override
    protected CarPledgePresenter initPresenter() {
        return new CarPledgePresenter();
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_bao_dan;
    }

    @Override
    public void initView() {
        initWidget();
        initData();
    }

    private void initData() {
        initListener();
        setAdapter();
        if (CarDataManager.getInstance().getCarDetailModel() != null && mPosition != YSConstant.INVALID) {
            reFreshList(CarDataManager.getInstance().getCarDetailModel());
        }
        if(mOperateType == CarPledgeActivity.TYPE_OPERATE_SEE) {
            moment_iv_edit.setVisibility(View.GONE);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        int i= 1;
    }

    @Override
    public void onResume() {
        super.onResume();
        reFreshList(CarDataManager.getInstance().getCarDetailModel());
    }

    private void initWidget() {
        mList = new ArrayList();
        mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
    }

    private void setAdapter() {
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initListener() {
        moment_iv_edit.setOnClickListener(this);

        mAdapter = new CommonRecyclerViewAdapter<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean>(getContext(), mList, R.layout.view_item_baodan) {
            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean vehicleSlipsBean = mList.get(position);
                ((CommonRecyclerViewHolder) holder).setText(R.id.baoxian_content, vehicleSlipsBean.getInsurance_company_desr());
                switch (vehicleSlipsBean.getGuarantee_slip_type()) {
                    case "1":
                        ((CommonRecyclerViewHolder) holder).setImgResource(R.id.baoxian_type, R.mipmap.jqx_icon);
                        break;
                    case "2":
                        ((CommonRecyclerViewHolder) holder).setImgResource(R.id.baoxian_type, R.mipmap.syx_icon);
                        break;
                    case "3":
                        ((CommonRecyclerViewHolder) holder).setImgResource(R.id.baoxian_type, R.mipmap.car_tax_icon);
                        break;

                }
                ((CommonRecyclerViewHolder) holder).setText(R.id.baodan_number, "保单号:" + vehicleSlipsBean.getGuarantee_slip_number());
                CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean.GuaranteeBeginBean guaranteeBeginBean = vehicleSlipsBean.getGuarantee_begin();
                CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean.GuaranteeEndBean guaranteeEndBean = vehicleSlipsBean.getGuarantee_end();
                String time = "" + YsUtil.getDateToString(guaranteeBeginBean.getTime()) + " ~ " + YsUtil.getDateToString(guaranteeEndBean.getTime());
                ((CommonRecyclerViewHolder) holder).setText(R.id.baoxian_date, time);
                if(mOperateType == CarPledgeActivity.TYPE_OPERATE_UPDATE) {
                    ((CommonRecyclerViewHolder) holder).setViewListener(R.id.delete);
                    ((CommonRecyclerViewHolder) holder).setViewListener(R.id.item);
                }
                if(mOperateType == CarPledgeActivity.TYPE_OPERATE_SEE) {
                    ImageView imageView = ((CommonRecyclerViewHolder)holder).getView(R.id.delete);
                    imageView.setVisibility(View.GONE);
                }


            }
        };

        mAdapter.setOnClickListener(new OnClickCommonListener() {
            @Override
            public void onClickListener(View view, int position) {
                CarDetailModel.BusiMortgageVoBean busiMortgageVoBean = null;
                switch (view.getId()) {
                    case R.id.delete:
                        String id = "";
                        busiMortgageVoBean = CarDataManager.getInstance().getCarDetailModel().getBusiMortgageVo();
                        if (busiMortgageVoBean != null) {
                            List<List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean>> lists = busiMortgageVoBean.getBusivehicleandslipvoList();
                            if (lists != null && lists.size() > 0) {
                                CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean busivehicleandslipvoListBeen = lists.get(mPosition).get(0);
                                List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean> vehicleSlipsBeen = busivehicleandslipvoListBeen.getVehicleSlips();
                                id = String.valueOf(vehicleSlipsBeen.get(position).getId());
                            }
                        }
                        final String baodan_id = id;
                        AlertTwoBtnDialog dialog = new AlertTwoBtnDialog(getContext(), new CustomAlertDialogCallBack() {
                            @Override
                            public void onCancelBtnClicked() {
                            }

                            @Override
                            public void onSureBtnClicked() {
                                deleteBaoDan(baodan_id);
                            }

                            @Override
                            public void onSureBtnClicked1(String editStr) {
                            }
                        });
                        dialog.setMessage(getString(R.string.baodan_dialog_delete));
                        break;


                    case R.id.item:
                        busiMortgageVoBean = CarDataManager.getInstance().getCarDetailModel().getBusiMortgageVo();
                        String vehicle_id = "";
                        if (busiMortgageVoBean != null) {
                            List<List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean>> lists = busiMortgageVoBean.getBusivehicleandslipvoList();
                            if (lists != null && lists.size() > 0) {
                                CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean busivehicleandslipvoListBeen = lists.get(mPosition).get(0);
                                vehicle_id = String.valueOf(busivehicleandslipvoListBeen.getVehicle_code());
                            }
                        }
                        if (!TextUtils.isEmpty(vehicle_id)) {

                            Bundle bundle = new Bundle();
                            bundle.putString(AddBaoDanActivity.PARAM_BAODAN_INDEX,String.valueOf(position));
                            bundle.putString(AddBaoDanActivity.PARAM_CAR_INDEX,String.valueOf(mPosition));
                            bundle.putString(AddBaoDanActivity.PARAM_VEHICLE_ID, vehicle_id);
                            ((CarUpdateActivity) getActivity()).skipToTargetPage(AddBaoDanActivity.class, bundle);
                        }
                        break;
                    default:break;
                }
            }
        });
        mAdapter.isHasFooter(false);
    }


    private void deleteBaoDan(String id) {
        if(!TextUtils.isEmpty(id)) {
            LoadingProgressUtils.showProgressDialog(getActivity());
            presenter.deleteBaoDan(id);
        }
    }

    public void setDataIndex(int position) {
        mPosition = position;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.moment_iv_edit:
                CarDetailModel.BusiMortgageVoBean busiMortgageVoBean = CarDataManager.getInstance().getCarDetailModel().getBusiMortgageVo();
                String vehicle_id = "";
                if (busiMortgageVoBean != null) {
                    List<List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean>> lists = busiMortgageVoBean.getBusivehicleandslipvoList();
                    if (lists != null && lists.size() > 0) {
                        CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean busivehicleandslipvoListBeen = lists.get(mPosition).get(0);
                        vehicle_id = String.valueOf(busivehicleandslipvoListBeen.getVehicle_code());
                    }
                }
                if (!TextUtils.isEmpty(vehicle_id)) {
                    Bundle bundle = new Bundle();
                    bundle.putString(AddBaoDanActivity.PARAM_VEHICLE_ID, vehicle_id);
                    ((CarUpdateActivity) getActivity()).skipToTargetPage(AddBaoDanActivity.class, bundle);
                }
                break;
        }
    }


    private void reFreshList(CarDetailModel carDetailModel) {
        if(carDetailModel == null) {
            return;
        }
        CarDetailModel.BusiMortgageVoBean busiMortgageVoBean = carDetailModel.getBusiMortgageVo();
        if (busiMortgageVoBean != null) {
            List<List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean>> lists = busiMortgageVoBean.getBusivehicleandslipvoList();
            if (lists != null && lists.size() > 0) {
                CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean busivehicleandslipvoListBeen = lists.get(mPosition).get(0);
                List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean.VehicleSlipsBean> vehicleSlipsBeen = busivehicleandslipvoListBeen.getVehicleSlips();
                mList.clear();
                mList.addAll(vehicleSlipsBeen);
                mAdapter.notifyDataSetChanged();
                return;
            }
        }
    }

    @Override
    public void onFail(String msg) {
        LoadingProgressUtils.closeDialog();
        ToastUtils.showToast(msg);
    }

    @Override
    public void onSuccess(int code) {
        switch (code) {
            case CarPledgeContract.DELETE_CAR_BAODAN:
                presenter.getCarInfoById(CarDataManager.getInstance().getLoanId());
                break;
            case CarPledgeContract.GET_CAR_DETAIL:
                LoadingProgressUtils.closeDialog();
                ToastUtils.showToast("删除成功");
                reFreshList(CarDataManager.getInstance().getCarDetailModel());
        }
    }

    public void setType(int type) {
        mOperateType = type;
    }
}
